package cn.edu.jxyy.batistest.test;

import cn.edu.jxyy.batistest.mapper.CustomerMapper;
import cn.edu.jxyy.batistest.pojo.Customer;
import cn.edu.jxyy.batistest.utils.MyBatisUtils;
import org.apache.ibatis.session.SqlSession;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Part16_TestForeach {

    public static void findByArrayTest() {
        // 获取SqlSession
        SqlSession sqlSession = MyBatisUtils.getSession();
        // 创建数组，封装查询id
        Integer[] roleIds = {2, 3};

        CustomerMapper customerMapper =
                sqlSession.getMapper(CustomerMapper.class);


        // 执行SqlSession的查询方法，返回结果集
        List<Customer> customers = customerMapper.findByArray(roleIds);
        // 输出查询结果信息
        for (Customer customer : customers) {
            // 打印输出结果
            System.out.println(customer);
        }
        // 关闭SqlSession
        sqlSession.close();
    }

    /**
     * 根据客户id批量查询客户信息
     */
    public static void findByListTest() {
        // 获取SqlSession
        SqlSession sqlSession = MyBatisUtils.getSession();
        // 创建List集合，封装查询id
        List<Integer> ids = new ArrayList<Integer>();
        ids.add(1);
        ids.add(2);

        // 执行SqlSession的查询方法，返回结果集
        CustomerMapper customerMapper =
                sqlSession.getMapper(CustomerMapper.class);
        List<Customer> customers = customerMapper.findByList(ids);


        // 输出查询结果信息
        for (Customer customer : customers) {
            // 打印输出结果
            System.out.println(customer);
        }
        // 关闭SqlSession
        sqlSession.close();
    }

    /**
     * 根据客户id批量查询客户信息
     */
    public static void findByMapTest() {
        // 获取SqlSession
        SqlSession sqlSession = MyBatisUtils.getSession();
        // 创建List集合，封装查询id
        List<Integer> ids = new ArrayList<Integer>();
        ids.add(1);
        ids.add(2);
        ids.add(3);
        Map<String, Object> conditionMap = new HashMap<String, Object>();
        conditionMap.put("id", ids);
        conditionMap.put("jobs", "teacher");
        // 执行SqlSession的查询方法，返回结果集
        CustomerMapper customerMapper =
                sqlSession.getMapper(CustomerMapper.class);
        List<Customer> customers =customerMapper.findByMap(conditionMap);
        // 输出查询结果信息
        for (Customer customer : customers) {
            // 打印输出结果
            System.out.println(customer);
        }
        // 关闭SqlSession
        sqlSession.close();
    }

    public static void main(String[] args){
        findByArrayTest();
        findByListTest();
        findByMapTest();
    }
}
